using System;
using System.Collections;
using System.Data;

using EntLibEC.Info.Online;
using EntLibEC.Dac.Online;
using EntLibEC.Cmn;
using EntLibEC.Dac;

namespace EntLibEC.Biz.Online
{
	/// <summary>
	/// Summary description for ProductRelatedManager.
	/// </summary>
	public class ProductRelatedManager
	{
		private ProductRelatedManager()
		{			
		}

		private static ProductRelatedManager _instance ; 
		public static ProductRelatedManager GetInstance()
		{
			if( _instance == null )
			{
				_instance = new ProductRelatedManager();
			}
			return _instance;
		}

		public DataSet GetRelatedProduct(Hashtable ht)
		{
		    string sql = @"select ProductSysNo , RelatedProductSysNo , p1.productname as ProductName ,p1.productid as ProductID ,Ent_Product_Related.sysno,
                           p2.productname as RelatedProductName ,p2.productid as RelatedProductID ,Ent_Product_Related.CreateTime , Ent_Sys_User.UserName 
                           from 
                               Ent_Product_Related
                           inner join Ent_Product as p1 on Ent_Product_Related.productsysno = p1.sysno 
                           inner join Ent_Product as p2 on Ent_Product_Related.relatedproductsysno = p2.sysno                 
                           inner join Ent_Sys_User on Ent_Product_Related.createusersysno = Ent_Sys_User.sysno 
 
                           where 1=1
                           @product
                           @relatedproduct
                           @pm
                           order by Ent_Product_Related.createtime desc ";

			if (ht.ContainsKey("ProductSysNo"))
				sql = sql.Replace("@product" , "and productsysno = " + ht["ProductSysNo"].ToString().Trim() );
			else
				sql = sql.Replace("@product" , "");

			if (ht.ContainsKey("RelatedProductSysNo")) 
				sql = sql.Replace("@relatedproduct" , " and relatedproductsysno = " + ht["RelatedProductSysNo"].ToString().Trim());
			else
				sql = sql.Replace("@relatedproduct" , "");

			if (ht.ContainsKey("PMSysNo"))
				sql = sql.Replace("@pm" , " and p1.pmusersysno = " + ht["PMSysNo"].ToString().Trim());
			else
				sql = sql.Replace("@pm" , "");

			if (ht == null || ht.Count ==0)
				sql = sql.Replace("select" , "select top 50 ");

			return SqlHelper.ExecuteDataSet(sql);

		}

		public void Insert(ProductRelatedInfo oParam)
		{
		    new ProductRelatedDac().Insert(oParam);
		}

		public void Delete(int sysno)
		{
		    new ProductRelatedDac().Delete(sysno);
		}
	}
}
